package com.tansuo365.test1.controller.kucunstuff;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tansuo365.test1.bean.kucunbean.WuliaoKucunRiZhi;
import com.tansuo365.test1.service.kucunservice.WuliaoKucunRiZhiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 统计库存查询使用
 * 在新物料表新增或者删除更改后,对应的进行更改
 * 当新增物料表时,新增不重复的物料进库存日志
 */
@Api(value = "物料库存日志控制层",  tags = "物料库存日志控制层", description = "")
@RequestMapping("/admin/wuliaorizhi")
@RestController
public class WuliaoKucunRiZhiController {
    @Autowired
    private WuliaoKucunRiZhiService wuliaoKucunRiZhiService;

    @ApiOperation("库存查询")
    @PostMapping("/getSelective")
    public Map<String,Object> getWuliaoRizhiSelective(WuliaoKucunRiZhi wuliaoKucunRiZhi,@RequestParam(value = "page", required = true) Integer page, @RequestParam(value = "rows", required = true) Integer rows){
        Map<String,Object> resultMap = new HashMap<>();
        if(wuliaoKucunRiZhi==null){
            resultMap.put("errorInfo","数据为空,无法选择");
            resultMap.put("success",false);
            return resultMap;
        }
        PageHelper.startPage(page,rows);
        List<WuliaoKucunRiZhi> kucunRiZhiSelective = wuliaoKucunRiZhiService.listKucunWithRiZhi(wuliaoKucunRiZhi);
        PageInfo pageInfo = new PageInfo(kucunRiZhiSelective);
        if(pageInfo.getList()!=null){
            resultMap.put("success",1);
            resultMap.put("rows",pageInfo.getList());
            resultMap.put("total",pageInfo.getTotal());
            return resultMap;
        }else{
            resultMap.put("errorInfo","分页数据获取失败");
            resultMap.put("success",0);
            return resultMap;
        }
    }

    @RequestMapping("save")
    public Map<String,Object> saveNewKucunRiZhi(WuliaoKucunRiZhi wuliaoKucunRiZhi){
        Map<String,Object> resultMap = new HashMap<>();
        if(wuliaoKucunRiZhi==null){
            resultMap.put("errorInfo","数据为空,无法新增");
            resultMap.put("success",false);
            return resultMap;
        }
        int insertCode = wuliaoKucunRiZhiService.insertSelective(wuliaoKucunRiZhi);
        if(insertCode>0){
            resultMap.put("success",true);
            resultMap.put("code",insertCode);
            return resultMap;
        }else{
            resultMap.put("success",false);
            resultMap.put("code",insertCode);
            return resultMap;
        }
    }
}
